package com.youmingxing.activity.selfInfo;

import android.os.Bundle;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.ContentListAdapter;
import com.youmingxing.bean.Content;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by JHS on 2015/10/17
 * 我的全部作品
 */
public class MyAllWorksActivity extends BaseActivity implements RefreshListView.OnLoadMoreListener {

    private ContentListAdapter adapter;
    private RefreshListView listView;
    private List<Content> contentList;
    int mark = 1;
    int userId = 1;
    int type = 0;
    String url;
    public final static int TYPE_USER_WORK = 0; //我的作品
    public final static int TYPE_GREAT_WORK = 1; //优秀作品
    public final static String EXTRAS_ID = "u_id";
    public final static String EXTRAS_TYPE = "type";
    public final static String EXTRAS_URL = "url";
    public final static String EXTRAS_TITLE = "title";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_my_all_work);
        contentList = new ArrayList<>();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            userId = bundle.getInt(EXTRAS_ID, -1);
            type = bundle.getInt(EXTRAS_TYPE, TYPE_USER_WORK);
            url = bundle.getString(EXTRAS_URL, "");
        }
        initView();
        if (type == TYPE_USER_WORK) {
            AllStarApi.getUserContents(userId, mark, contentHandler);
        } else if (type == TYPE_GREAT_WORK) {
            AllStarApi.getGreateContent(url, -1, -1, contentHandler);
            setTopbarName(bundle.getString(EXTRAS_TITLE, ""));
        }
    }

    @Override
    public void onErrorClick() {
        AllStarApi.getUserContents(userId, mark, contentHandler);
    }

    private void initView() {
        if (type == TYPE_USER_WORK) {
            setTopbarName(R.string.title_activity_my_work);
        } else if (type == TYPE_GREAT_WORK) {
            setTopbarName(R.string.title_activity_my_work2);
        }
        listView = (RefreshListView) findViewById(R.id.list_my_work);
        listView.setOnLoadMoreListener(this);
        adapter = new ContentListAdapter(this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    BaseJsonHandler contentHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            showErrorPage(false);
            JSONArray data = response.optJSONArray("data");
            if (mark == 1 && data.length() == 0) {
                listView.noData("您当前还没有作品哦");
            }
            mark++;
            if (data.length() < 20) {
                listView.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                Content content = new Content(data.optJSONObject(i));
                if (type == TYPE_USER_WORK) {
                    //个人作品可以删除
                    content.u_apply_id = -1;
                    content.isShowDelete = true;
                }
                contentList.add(content);
            }
            adapter.setData(contentList);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            listView.endLoadMore();
            if (contentList == null || contentList.size() == 0) {
                showErrorPage(true);
                listView.setFootVisible(false);
            }
        }
    };

    @Override
    public void onLoadMore() {
        if (type == TYPE_USER_WORK) {
            AllStarApi.getUserContents(userId, mark, contentHandler);
        } else {
            Content lastItem = contentList.get(contentList.size() - 1);
            AllStarApi.getGreateContent(url, lastItem.getCreatetime(), lastItem.getId(),
                    contentHandler);
        }
    }
}